<?php

namespace App\Models\OpenShop\InvestmentPromotion;

use App\Enums\OpenShop\InvestmentPromotion\UploadFileResourceEnum;
use App\Models\BaseModel;
use App\Models\Traits\KfSoftDeletes;
use DateTimeInterface;

class OpenShopSpaceDesignModel extends BaseModel
{
    use KfSoftDeletes;

    protected $table = 'open_shop_space_design';

    protected $fillable = [
        'mainProcessId',
        'step',
        'orderNo',
        'designUid',
        'designName',
        'preparationUid',
        'preparationName',
        'openTime',
        'isDeleted',
        'createdAt',
        'createdByUid',
        'updatedAt',
        'updatedAtByUid',
    ];


    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format("Y-m-d H:i:s");
    }

    public static function setData($orderNo, $data)
    {
        self::query()
            ->where('orderNo', $orderNo)
            ->update($data);
    }

    public function costMappingRel()
    {
        return $this->hasMany(OpenShopSpaceDesignCostMappingModel::class, 'orderNo', 'orderNo');
    }

    public function softMappingRel()
    {
        return $this->hasMany(OpenShopSpaceDesignSoftOutfitMappingModel::class, 'orderNo', 'orderNo');
    }

    public static function getDesignInfo($orderNo)
    {
        return self::query()
                   ->where('orderNo', $orderNo)
                   ->with([
                       'designMappingRel' => function ($query) {
                           $query->where('isDeleted', 0);
                       }
                   ])
                   ->first();
    }

    public static function deletedInfo($orderNo)
    {
        self::query()
            ->where('orderNo', $orderNo)
            ->update(['isDeleted' => 1]);
    }

    /**
     * 获取信息
     *
     * @param string $orderNo
     * @param array $field
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     */
    public function getInfo(string $orderNo, $field = [])
    {
        return self::query()
                   ->where('orderNo', $orderNo)->select($field)
                   ->first();
    }

    /**
     * 指派人信息
     *
     * @param string $orderNo
     * @return int
     */
    public function getAssignUid(string $orderNo)
    {
        return (int)optional(self::getInfo($orderNo, ['designUid']))->designUid;
    }

    public static function getUids($orderNo, $step, $name)
    {
        return self::query()
                   ->where('orderNo', $orderNo)
                   ->where('step', $step)
                   ->pluck("{$name}")
                   ->toArray();
    }

    /**
     * 更新step
     * @param string $orderNo
     * @param int $step
     * @return bool
     */
    public static function updateStep(string $orderNo, int $step): bool
    {
        $design = self::query()->where('orderNo', $orderNo)->first();
        if (!$design) {
            return false;
        }

        $design->step = $step;
        return $design->save();
    }
}
